from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
# link: 为什么需要激励函数
from keras.layers import Dense, Activation
# Optimizer that implements the RMSprop algorithm.
from keras.optimizers import RMSprop

(X_train, y_train), (X_test, y_test) = mnist.load_data()

# 视频链接：为什么要特征标准化？
# data pre-processing
X_train = X_train.reshape(X_train.shape[0], -1) / 255 # normalize
X_test = X_test.reshape(X_test.shape[0], -1) / 255 # normalize
# one-hot 化
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)

# Another way to build your neural net
model = Sequential([
    # 输入的特征有 28*28, 输出的特征有 32
    Dense(32, input_dim=28*28),
    Activation('relu'),
    Dense(10),
    Activation('softmax')
])

# Another way to define optimizer
rmsprop = RMSprop(learning_rate=0.001, rho=0.9, epsilon=1e-08,decay=0.0)

# we add metrics to get more results you want to see
model.compile(optimizer=rmsprop, loss='categorical_crossentropy', metrics=['accuracy'])

print("\nTraining ------------------------")
# Another way to train the model
model.fit(X_train,y_train, epochs=2, batch_size=32)

print("\nTesting --------------------------")
# Evaluate the model with the metrics we defined earlier
loss, accuracy = model.evaluate(X_test, y_test)

print("test loss: ", loss)
print("test accuracy: ", accuracy)

